package cn.edu.lit.jsjxy.CourseExample.chapter5;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

import cn.edu.lit.jsjxy.CourseExample.R;

public class NotificationTestActivity extends Activity implements View.OnClickListener
{
    NotificationManager n_Manager;
    Notification notification;
    Button btn1, btn2;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_test);

        String service = NOTIFICATION_SERVICE;
        n_Manager = (NotificationManager) getSystemService(service);

        btn1 = (Button) findViewById(R.id.btnShowNotificcation);
        btn2 = (Button) findViewById(R.id.btnRemoveNotificcation);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        if (view == btn1)
        {
            int icon = R.mipmap.ic_launcher_round;
            CharSequence tickerText = "紧急通知,程序已启动";
            long when = System.currentTimeMillis();
            //notification = new Notification(icon, tickerText, when);
            //notification = new NotificationCompat.Builder(this).build();

            Intent intent = new Intent(NotificationTestActivity.this, NotificationTestActivity
                    .class);
            PendingIntent pi = PendingIntent.getActivity(NotificationTestActivity.this, 0,
                    intent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

            builder.setSmallIcon(icon)
                    .setContentTitle(tickerText)
                    .setContentText(tickerText)
                    .setContentIntent(pi);

            notification = builder.build();

            //notification.setLatestEventInfo(this, "通知", "通知内容", pi);

            n_Manager.notify(10, notification );
        } else if (view == btn2)
        {
            n_Manager.cancel(10);


        }
    }
}
